package com.my.chapter17;

import net.mindview.util.Countries;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class practice20 {
    public static void main(String[] args) {
        SimpleHashMap2<String,String> m =
                new SimpleHashMap2<String,String>();
        m.putAll(Countries.capitals(6));
        m.putAll(Countries.capitals(6));
        System.out.println(m);
    }
}

class SimpleHashMap2<K,V> extends SimpleHashMap<K,V>{
    @Override
    public V put(K key, V value) {
        V oldValue = null;
        int index = Math.abs(key.hashCode()) % SIZE;
        MapEntry<K,V> pair = new MapEntry<K,V>(key, value);
        if(buckets[index] == null)
            buckets[index] = new LinkedList<MapEntry<K,V>>();
        else{
            System.out.print(
                    "Collision while adding\n" + pair
                            + "\nBucket already contains:");
            Iterator<MapEntry<K,V>> it =
                    buckets[index].iterator();
            while(it.hasNext())
                System.out.print(it.next() + " ");
            System.out.println("");
        }
        LinkedList<MapEntry<K,V>> bucket = buckets[index];
        boolean found = false;
        ListIterator<MapEntry<K,V>> it = bucket.listIterator();
        while(it.hasNext()) {
            MapEntry<K,V> iPair = it.next();
            if(iPair.getKey().equals(key)) {
                oldValue = iPair.getValue();
                it.set(pair); // Replace old with new
                found = true;
                break;
            }
        }
        if(!found)
            buckets[index].add(pair);
        return oldValue;
    }
}
